





//
//  SYDLoadView.m
//  LantaiyuanBus
//
//  Created by lantaiyuan on 2017/4/6.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDLoadView.h"
#import <QuartzCore/QuartzCore.h>
#define conerViewWidth 120
#define conerViewHeight 76

@implementation SYDLoadView

static SYDLoadView *myLoadView = nil;

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

-(void)showLoadView{
    if ([UIApplication sharedApplication].keyWindow.rootViewController.navigationController) {
        [[UIApplication sharedApplication].keyWindow.rootViewController.navigationController.view addSubview:self];
    }else{
    
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }

}
-(void)closeLoadView{
    [self removeFromSuperview];
}
+(SYDLoadView *)shareLoadView{

    @synchronized (self) {
        if (myLoadView == nil) {
            myLoadView = [[self alloc]initIsLikeSynchro:NO];
        }
    }
    return myLoadView;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    @synchronized (self) {
        if (myLoadView == nil) {
            myLoadView = [super allocWithZone:zone];
            return myLoadView;
        }
    }
    return  nil;
}
///初始化加载框，这个函数是表示LoadingView的大小，如果是Yes，则loadView的大小为整个窗体，在这种情况下网络请求的时候会遮盖整个窗体，用户其他操作都是无效的相当于同步，如果是No，则loadView的大小为为150*80，用户的其他操作是有效的，这种情况相下需要保证loadingView唯一；
-(id)initIsLikeSynchro:(BOOL)sLikeSynchro{
    if (_isLikeSynchro) {
        self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    }else{
        self = [super initWithFrame:CGRectMake((SCREEN_WIDTH - conerViewWidth)/2, ([UIApplication sharedApplication].keyWindow.bounds.size.height - conerViewHeight)/2, conerViewWidth, conerViewHeight)];
    }
    
    if (self) {
        self.isLikeSynchro = _isLikeSynchro;
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        
        conerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, conerViewWidth, conerViewHeight)];
        
        [self setCenter:conerView withParentRect:self.frame];
        UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65];
        conerView.backgroundColor = color;
        [self addSubview:conerView];
        
        indicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(42.5, 0, 35, 35)];
        [conerView addSubview:indicatorView];
        
        [indicatorView startAnimating];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 36, conerViewWidth, 25)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"正在努力加载中···";
        titleLabel.font = [UIFont systemFontOfSize:13];
        [conerView addSubview:titleLabel];
        
        conerView.layer.cornerRadius = 8;
        conerView.layer.masksToBounds = YES;
    }
    
    return  self;
}

/*设置子view在父view中居中*/
-(void)setCenter:(UIView *)child withParentRect:(CGRect)parentRect{
    CGRect rect = child.frame;
    rect.origin.x = (parentRect.size.width - child.frame.size.width)/2;
    rect.origin.y = (parentRect.size.height - child.frame.size.height)/2;
    child.frame = rect;
}

@end

